﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using HtmlDocument = HtmlAgilityPack.HtmlDocument;

namespace _10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //异步执行
        private async void button1_Click(object sender, EventArgs e)
        {
            string text = textBox1.Text;
            textBox2.Clear();
            textBox3.Clear();

            await SearchAsync(text);
        }

        private async Task SearchAsync(string text)
        {
            await Task.WhenAll(SearchEngineAsync(text, "sogou", textBox2),
                SearchEngineAsync(text, "bing", textBox3));
        }
        private async Task SearchEngineAsync(string keyword, string engineName, TextBox outputTextBox)
        {
            string url = "";
            switch (engineName)
            {
                //百度sogou因为防爬虫实际内容无法获取,bing也需要使用代理才能成功获取，所以实际可能是没用的
                case "sogou":
                    url = $"https://www.sogou.com/web?query={keyword}";
                    break;
                case "bing":
                    url = $"https://www.bing.com/search?q={keyword}";
                    break;
            }

            using (HttpClient client = new HttpClient())
            {
                HttpResponseMessage responseMessage = await client.GetAsync(url);
                if (responseMessage.IsSuccessStatusCode)
                {
                    HtmlWeb web = new HtmlWeb();
                    HtmlDocument doc = web.Load(url);

                    string xpath = "";
                    switch (engineName)
                    {
                        
                        case "sogou":
                            xpath = "/html/body/div[3]/div[2]/div[1]/div[3]/div/div[1]/div[1]/div/div[2]/p";
                            break;
                        case "bing":
                            xpath = "/html/body/div[1]/main/ol/li[2]/div[2]/p";
                            break;
                    }

                    HtmlNodeCollection resultNodes = doc.DocumentNode.SelectNodes(xpath);
                    if (resultNodes != null)
                    {
                        foreach (HtmlNode resultNode in resultNodes)
                        {
                            outputTextBox.AppendText(resultNode.OuterHtml);
                        }
                    }
                    else
                    {
                        outputTextBox.AppendText("No results found.\n\n");
                    }
                }
                else
                {
                    outputTextBox.AppendText("Search failed.\n\n");
                }
            }
        }
        
        
    }
}